package com.yyw.foodie.controller.center;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yyw.foodie.bo.orders.CommentsOrderItemBo;
import com.yyw.foodie.common.Result;
import com.yyw.foodie.controller.base.BaseController;
import com.yyw.foodie.pojo.OrderItemsEntity;
import com.yyw.foodie.pojo.OrdersEntity;
import com.yyw.foodie.service.ItemsCommentsService;
import com.yyw.foodie.service.OrderItemsService;
import com.yyw.foodie.service.OrderStatusService;
import com.yyw.foodie.utils.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 评价表
 * </p>
 *
 * @author yiyw
 * @since 2021-05-22
 */
@Slf4j
@Api(value = "用户中心评价相关", tags = "用户中心评价模块相关接口")
@RestController
@RequestMapping("/mycomments")
public class CommentsController extends BaseController {

    @Autowired
    private ItemsCommentsService commentsService;

    @Autowired
    private OrderItemsService orderItemsService;

    @Autowired
    private OrderStatusService orderStatusService;

    @ApiOperation(value = "根据用户ID和订单ID查询可以评价的订单列表")
    @PostMapping("/pending")
    public Result getPendingByOrderId(@ApiParam(value = "订单ID") @RequestParam("orderId") String orderId,
                                      @ApiParam(value = "用户ID") @RequestParam("userId") String userId) {
        if (StringUtils.isBlank(orderId) || StringUtils.isBlank(userId)) {
            return Result.errorMsg("参数非法");
        }
        // 检验是否可操作
        OrdersEntity entity = checkUserIsOperatingByComments(userId, orderId);
        //  获取可以评价的订单信息
        QueryWrapper<OrderItemsEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(OrderItemsEntity::getOrderId, orderId);
        List<OrderItemsEntity> list = orderItemsService.list(queryWrapper);

        return Result.ok(list);
    }


    @ApiOperation(value = "提交评价")
    @PostMapping("/saveList")
    public Result saveComment(@ApiParam(value = "订单ID") @RequestParam("orderId") String orderId,
                              @ApiParam(value = "用户ID") @RequestParam("userId") String userId,
                              @RequestBody List<CommentsOrderItemBo> commentsOrderItemBoList) {
        if (StringUtils.isBlank(orderId) || StringUtils.isBlank(userId)) {
            return Result.errorMsg("参数非法");
        }
        // 检验是否可操作
        checkUserIsOperatingByComments(userId, orderId);
        // 批量保存评价信息
        commentsService.saveComments(commentsOrderItemBoList, orderId, userId);
        return Result.ok();
    }


    @ApiOperation(value = "分页获取用户的订单信息", notes = "可以根据订单状态获取订单的信息")
    @PostMapping("/query")
    public Result getCommentsByPage(@ApiParam(value = "用户ID", required = true) @RequestParam("userId") String userId,
                                 @ApiParam(value = "当前页", defaultValue = "1")
                                 @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                 @ApiParam(value = "显示条数", defaultValue = "10")
                                 @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        if (StringUtils.isBlank(userId)) {
            return Result.errorMsg("非法参数，缺少用户ID");
        }
        PagedGridResult commentsInfo = commentsService.getCommentInfoByPage(userId, page, pageSize);
        return Result.ok(commentsInfo);
    }


}

